<?php
include_once 'parentPathConfig.php';
include_once $path . '/config.php';
include_once ROOT . '/utils/mysql_class.php';
include_once ROOT . '/models/News.php';
include_once ROOT . '/utils/Function.php';
class NewService{
	
	public function addNews(News $news)
	{
		$sql = "insert into news (news_title,news_content,news_date,news_department) values('".$news->getNews_title()."','".$news->getNews_content()."','".$news->getNews_date()."','".$news->getNews_department()."')";
		$result = mysql_query($sql);
		if($result)
		{
			return true;
		}
		else {
			return false;
		}
	}
	public function deleteNewses($newsIdList)
	{
		$count = 0;
		for($i=0; $i<count($newsIdList);$i++)
		{
			$count++;
			$sql = "delete from news where id='".$newsIdList[$i]."'";
			mysql_query($sql);
		}
		if($count==count($newsIdList))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	public function getAllList()
	{
		$sql = "select * from news order by id desc";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		while($row)
		{
			$news = new News();
			$news->setNews_content($row['news_content']);
			$news->setNews_id($row['id']);
			$news->setNews_title($row['news_title']);
			$news->setNews_date($row['news_date']);
			$news->setNews_department($row['news_department']);
		    $newsList[] = $news;
		    $row = mysql_fetch_array($result);
		}
		return $newsList;
	}
	public function  getListForPage($start, $end)
	{
		$sql = "select * from news order by id desc limit $start,$end";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		while($row)
		{
			$news = new News();
			$news->setNews_content($row['news_content']);
			$news->setNews_id($row['id']);
			$news->setNews_title($row['news_title']);
			$news->setNews_date($row['news_date']);
			$news->setNews_department($row['news_department']);
		    $newsList[] = $news;
		    $row = mysql_fetch_array($result);
		}
		return $newsList;
	}
	public  function getNewsById($newsId)
	{
		$sql = "select * from news where id='$newsId'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		if($row)
		{
			$news = new News();
			$news->setNews_content($row['news_content']);
			$news->setNews_id($row['id']);
			$news->setNews_title($row['news_title']);
			$news->setNews_date($row['news_date']);
			$news->setNews_department($row['news_department']);
		}
		return $news;
	}
	public function getLabelCount()
	{
		$sql = "select count(*) from news";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		if($row)
		{
			return $row[0];
		}
		else
		{
			return 0;
		}
	}
	public function updateNews(News $news)
	{
		$sql = "update news set  news_title='".$news->getNews_title()."',news_content='".$news->getNews_content()."',news_date='".$news->getNews_date()."',news_department='".$news->getNews_department()."' where id='".$news->getNews_id()."'";
		$result = mysql_query($sql);
		if($result)
		{
			return true;
		}
		else{
			return false;
		}
	}
}
?>